Repository navigation
fix(cli): exit after capture completes - #94
Merged
Merged
Conversation
The 2.0 rewrite dropped the Darwin.exit call that followed capture in the CLI, and capture() no longer reported success. After a capture the process parked in dispatchMain() forever, so every invocation had to be killed manually and repeated Alfred invocations broke. dispatchMain() also terminated the main thread, producing the CFRunLoop "main thread has exited" warning on startup. Restore capture()'s Bool result (discardable, so app call sites are unchanged), make the CLI exit 1 when capture produces no text, exit 0 once the command finishes, and keep the main thread alive with RunLoop.main.run() instead of dispatchMain().
This was referenced Sep 26, 2026
Review findings on #94: runShortcut spawned its child inside a detached task, so the CLI's new prompt exit could terminate the process before the shortcut ever ran; the spawn is now synchronous with cleanup in a termination handler. captureSingle and captureMultiRegion returned success for whitespace-only OCR results, so the CLI exited 0 with no text; both now treat empty results as failure.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause
The 2.0 rewrite dropped the
Darwin.exit(success ? 0 : 1)call that followedcapture()in the CLI (1.x had it), andcapture()stopped returning a success flag. After a capture theTaskwrappingtrex.main()completed but nothing terminated the process, so it parked indispatchMain()forever.dispatchMain()also terminates the main thread, which is what produced theCFRunLoopError_MainThreadHasExitedwarning.Fix
capture()'sBoolresult in TRexCore (@discardableResult, so all app call sites are unchanged)ExitCode.failurewhen capture produces no text (user cancelled, no text recognized, image unreadable), sotrexexits 1; parse/stdin errors already exited via ArgumentParsertrex.main()returnsdispatchMain()withRunLoop.main.run()so the main thread stays alive to service@MainActorwork; this also removes the run loop warningVerification
trex --helpexits 0trex -i < /dev/nullexits 1 with error messagetrex -i < image.pngprints recognized text, exits 0, no run loop warning; two back-to-back invocations both work (the Alfred case)xcodebuild -scheme "TRex CLI"and-scheme TRexDebug builds succeedswift test --package-path Packages/TRexCore: 21 tests pass, including two new regression tests for the restoredcapture()return value; the exit logic itself lives in the CLI entry point, which has no test targetInteractive screen capture (
trexwith no flags) was not exercised here since it needs screen recording permission and a user selection; it goes through the samecapture()/exit path.Fixes #93